package cn.epsilon3.handler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.epsilon3.exception.Epsilon3ResultException;
import cn.epsilon3.exception.Epsilon3ReturnException;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.epsilon3.common.Result;
import cn.epsilon3.constant.ResultEnum;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常捕捉处理器
 *
 * @author epsilon3
 * @date 2023-05-08
 */
@ControllerAdvice
public class Epsilon3ExceptionHandler {

    private static final Log log = LogFactory.get();

    private static final String projectName = "千渝掌柜开放平台";

    /**
     * 捕捉异常返回前端
     *
     * @param e 返回信息
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Epsilon3ReturnException.class)
    public Result<String> epsilon3ReturnExceptionResult(Epsilon3ReturnException e) {
        return new Result<>(ResultEnum.ERROR,e.getMessage());
    }

    /**
     * 返回需要2fa信息给前端
     *
     * @param e 返回信息
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Epsilon3ResultException.class)
    public Result<String> epsilon3Need2FaReturnException(Epsilon3ResultException e) {
        return new Result<>(e.getResultEnum());
    }

    /**
     * 无权限异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(NotPermissionException.class)
    public Result<String> handlerException(NotPermissionException e) {
        return new Result<>(ResultEnum.NO_PERMISSION);
    }

    /**
     * 无角色异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(NotRoleException.class)
    public Result<String> handlerException(NotRoleException e) {
        return new Result<>(ResultEnum.NO_PERMISSION);
    }

    /**
     * 未登陆异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(NotLoginException.class)
    public Result<String> handlerException(NotLoginException e) {
        return new Result<>(ResultEnum.AUTH_EXPIRED);
    }

    /**
     * 参数为空异常
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> handlerException(MethodArgumentNotValidException e) {
        return new Result<>(ResultEnum.ERROR,
                e.getBindingResult()
                        .getAllErrors()
                        .stream()
                        .map(ObjectError::getDefaultMessage)
                        .reduce((key,value) -> value)
                        .orElse("参数错误"));
    }

    /**
     * 捕捉异常返回前端
     *
     * @param e 返回信息
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BindException.class)
    public Result<String> epsilon3BindExceptionResult(BindException e) {
        return new Result<>(ResultEnum.ERROR,
                e.getBindingResult()
                        .getAllErrors()
                        .stream()
                        .map(ObjectError::getDefaultMessage)
                        .reduce((key,value) -> value)
                        .orElse("参数错误"));
    }

}
